package org.jbit.biz;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jbit.bean.DictionaryHelp;
import org.jbit.bean.User;
import org.jbit.dao.AddressDao;
import org.jbit.dao.UserDao;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * Servlet implementation class GPSInfoListAction
 */
@WebServlet("/UserListAction")
public class UserListAction extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public UserListAction() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub

		String method = request.getParameter("method");
		if (method.equals("list")){
			
			String rows = request.getParameter("rows");
			String page = request.getParameter("page");
			String sidx = request.getParameter("sidx");
			String sord = request.getParameter("sord");
			String kw = request.getParameter("kw");
			String ci = request.getParameter("ci");
			
			UserDao UD = new UserDao();
			List<User> user=new ArrayList<User>();
			String totalRecord="0";
			AddressDao AD=new AddressDao();
			
			user = UD.getUser(rows, page, sidx, sord, kw, ci);		
			totalRecord =UD.getUserTotal(kw, ci);
			List<DictionaryHelp> listUser=new ArrayList<DictionaryHelp>();;
			listUser=UD.getUserDictionary();
			
			for(int i=0;i<user.size();i++)
				for (int j=0;j<listUser.size();j++){
			if (user.get(i).getType().equals(listUser.get(j).getKey()))
			{
			user.get(i).setTypeId(user.get(i).getType());
			user.get(i).setType(listUser.get(j).getValue());}
			user.get(i).setCountyId(user.get(i).getCountyId());
			user.get(i).setCounty(AD.getAdderssById(user.get(i).getCountyId()));				
				}
			
			int totalPage = 0;
			try { 
			if (Integer.valueOf(totalRecord) % Integer.valueOf(rows) == 0) {
				totalPage = Integer.valueOf(totalRecord) / Integer.valueOf(rows);
			} else {
				totalPage = Integer.valueOf(totalRecord) / Integer.valueOf(rows)
						+ 1;
			}
			JSONObject jsonObject = new JSONObject();		
			jsonObject.accumulate("gridModel",JSONArray.fromObject(user));
			jsonObject.accumulate("total", ""+totalPage);
			jsonObject.accumulate("page", page);
			jsonObject.accumulate("record", totalRecord);		
					
			String contentType = "application/json;charset=utf-8";
			response.setHeader("Cache-Control", "no-cache");
			response.setContentType(contentType);
			PrintWriter pw = response.getWriter();
			pw.write(jsonObject.toString());
			pw.flush(); 
			pw.close();	
			} catch (Exception ex) { 
			} 
		}
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
	}
}
